<?php
namespace app\api\controller;
use app\common\controller\Api;

class News extends Api
{
    public function getOne()
    {
        $news = model('News')->where(['status' => 1])->find();
        $news['content'] = msubstr(strip_tags($news['content']), 0, 70);
        return $news;
    }

    public function getList($page = 1, $size = 3) {
        $news = model('News')->where(['status' => 1])->order('sort asc')->paginate($size, true, ['page' => $page]);
        if ($news->isEmpty())
        {
            return [
                'current_page' => $news->currentPage(),
                'data' => []
            ];
        }else{
            $data = $news->toArray();
            $re = [];
            foreach ($data['data'] as $k => $v) {
                $re[$k]['id'] = $v['id'];
                $re[$k]['title'] = $v['title'];
                $re[$k]['create_time'] = date('Y-m-d', strtotime($v['create_time']));
                $re[$k]['sort'] = $v['sort'];
                $re[$k]['cover'] = get_cover($v['cover'], 'url');
                $re[$k]['content'] = msubstr(strip_tags($v['content']), 0, 70);
            }
            return [
                'current_page' => $news->currentPage(),
                'data' => $re
            ];
        }
    }

    public function info($id)
    {
        $news = model('News')->where(['status' => 1, 'id' => $id])->find();
        $news['content'] = htmlentities($news['content']);
        return $news;
    }
}
